package com.share.device.service.impl;

import cn.hutool.core.util.StrUtil;
import com.share.device.domain.Region;
import com.share.device.domain.Station;
import com.share.device.mapper.RegionMapper;
import com.share.device.service.IRegionService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements IRegionService {
//    @Autowired
//    private RegionMapper regionMapper;

//    @Override
//    public List<Region> treeSelect(String parentCode) {

    /// /        List<Region> regionList = regionMapper.selectList(new LambdaQueryWrapper<Region>().eq(Region::getParentCode, parentCode));
    /// /        if (!CollectionUtils.isEmpty(regionList)) {
    /// /            regionList.forEach(item -> {
    /// /                long count = regionMapper.selectCount(new LambdaQueryWrapper<Region>().eq(Region::getParentCode, item.getCode()));
    /// /                if (count > 0) {
    /// /                    item.setHasChildren(true);
    /// /                } else {
    /// /                    item.setHasChildren(false);
    /// /                }
    /// /            });
    /// /        }
//        List<Region> list = query().eq(Region::getParentCode,parentCode).list();
//        return regionList;
//    }
    @Override
    public List<Region> treeSelect(String parentCode) {
        List<Region> list = query().eq("parent_code", parentCode).list();
        for (Region region : list) {
            String code = region.getCode();
            Long count = query().eq("parent_code", code).count();
            if (count > 0) {
                region.setHasChildren(true);
            } else {
                region.setHasChildren(false);
            }
        }
        return list;
    }

    @Override
    public  String getNameByCode(String code) {
        if (StrUtil.isBlank(code)) {
            return "";
        }
        Region region = query().eq(StrUtil.isNotBlank(code), "code", code).one();
        return StrUtil.isBlank(region.getCode()) ? "" : region.getCode();
    }

    @Override
    public String getFullNameByStation(Station station) {
        String cityCode = station.getCityCode();
        String districtCode = station.getDistrictCode();
        String provinceCode = station.getProvinceCode();
        StringBuilder fullStr = new StringBuilder(getNameByCode(provinceCode) + getNameByCode(districtCode) + getNameByCode(cityCode));
        return fullStr.toString();
    }
}